﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NTQOnlineExam.Services.Infrastructure;
using NTQOnlineExam.Services.Models;

namespace NTQOnlineExam.Services.DBCommand.Section
{
    public class GetMaxOrderSectionByTermDbCommand : ICommand<int>
    {
        private int _termId;
        public GetMaxOrderSectionByTermDbCommand(int termId)
        {
            _termId = termId;
        }
        private const string SQL = @"
           Select MAX([Order]) from dbo.Section WHERE TermId = @TermId
        ";

        public int Execute()
        {
            var connectionString = ConfigurationSettings.AppSettings["ConnectionString"];

            using (var con = new SqlConnection(connectionString))
            {
                using (var cmd = new SqlCommand(SQL, con))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@TermId", SqlDbType.Int).Value = _termId;
                    con.Open();
                    object result =cmd.ExecuteScalar();
                    if (result == null) return 0;
                    return (int) result;
                }
            }
        }

       
    }
}
